package com.hmsm.sys.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hmsm.server.base.BaseAction;
import com.hmsm.sys.bean.DeptTypeEnum;
import com.hmsm.sys.dto.DeptInfoDto;
import com.hmsm.sys.dto.UserDto;
import com.hmsm.sys.po.DeptInfo;
import com.hmsm.sys.po.OrgDeptLevel;
import com.hmsm.sys.po.OrgInfo;
import com.hmsm.sys.po.OrgType;
import com.hmsm.sys.po.UserInfo;
import com.hmsm.sys.service.DeptInfoService;
import com.lzyyj.common.Page;

@SuppressWarnings("serial")
public class DeptInfoAction extends BaseAction<UserInfo> {
	
	@Resource
	private DeptInfoService deptInfoService;
	
	public DeptInfoService getDeptInfoService() {
		return deptInfoService;
	}

	public void setDeptInfoService(DeptInfoService deptInfoService) {
		this.deptInfoService = deptInfoService;
	}

	private String nowpage = null;// 页码
	private String maxpage = null;// 每页显示条数	
	private List<DeptInfo> result;
	private int orgId;
	private Integer deptId;
	private Integer parId;
	private List<OrgType> orgTypeList;
	private List<OrgDeptLevel> orgDeptLevelList;
	
	public Integer getParId() {
		return parId;
	}

	public void setParId(Integer parId) {
		this.parId = parId;
	}
	
	private Integer deptType;

	public Integer getDeptType() {
		return deptType;
	}

	public void setDeptType(Integer deptType) {
		this.deptType = deptType;
	}

	private String name;
	private String dname;
	private int dpIpId;
	private List<OrgInfo> orginfolist;
	private List<DeptInfo> deptInfolist;
	private String orgInfoJson;
	private List<UserDto> userBeanAll;
	private List<DeptInfoDto> deptInfoBeans;
	private String ipAddress;
	private List<UserDto> deptUsers;
	private Integer type;
	private String deptNo;
	private DeptInfo deptInfo;
	private int userId;
	
	private List<DeptInfo> parentSelectList;
	
	public List<DeptInfo> getParentSelectList() {
		return parentSelectList;
	}

	public void setParentSelectList(List<DeptInfo> parentSelectList) {
		this.parentSelectList = parentSelectList;
	}
	
	public DeptTypeEnum[] getDeptTypes(){
		return DeptTypeEnum.values();
	}
	
	private String definedResult;
	
	public String getDefinedResult() {
		return definedResult;
	}

	protected Page<DeptInfo> page = new Page<DeptInfo>();
	
	public Page<DeptInfo> getPage() {
		return page;
	}

	public void setPage(Page<DeptInfo> page) {
		this.page = page;
	}

	private String itemPropertyList;

	public String getItemPropertyList() {
		return itemPropertyList;
	}

	public void setItemPropertyList(String itemPropertyList) {
		this.itemPropertyList = itemPropertyList;
	}
	
	private Boolean ajax;

	public boolean isAjax() {
		if (ajax==null){
			HttpServletRequest request= (HttpServletRequest)ServletActionContext.getRequest();;   
			ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
			
		}
		return ajax;
	}

	public void setAjax(boolean ajax) {
		this.ajax = ajax;
	}

	public DeptInfoAction(){
		page=new Page<DeptInfo>(10);
		page.setFindByExample(new DeptInfo());
	}
	
	public String manage(){
		if (deptId!=null){
			if (page.getFindByExample().getParent()==null){
				page.getFindByExample().setParent(new DeptInfo(deptId));
			}else{
				page.getFindByExample().getParent().setPkId(deptId);
			}
		}
		try {
			page = getDeptInfoService().manage(page);
		} catch (Exception e) {
			addActionError("发生错误："+e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 机构管理新增初化始
	 * @return
	 */
	public String add(){
		try {
			DeptInfo parent = getDeptInfoService().getById(deptInfo.getParId());
			
			deptInfo = new DeptInfo();
			deptInfo.setParent(parent);
			deptInfo.setParId(parent.getPkId());
			loadParamList(deptInfo);
			definedResult = "edit";
			return "definedResult";
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError("发生错误："+e);
			return ERROR;
		}
	}
	
	/**
	 * 获取部门信息以便编辑
	 * @author zhangshuai
	 * @date 2013年7月2日 17:08:54
	 */
	public String edit(){
		setItemPropertyList("name,parent,deptType,org.name,deptNo,relevanceConfig");
		deptInfo= getDeptInfoService().getById(deptInfo.getPkId());
		try {
			loadParamList(deptInfo);
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError("发生错误："+e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 删除部门
	 * @author lzyyj
	 * @date 2016年11月23日 17:08:54
	 */
	public String del(){
		String result = SUCCESS;
		
		deptInfo = getDeptInfoService().getById(deptInfo.getPkId());
		if (deptInfo==null){
			addActionError("要删除的部门不存在！");
			result = ERROR;
		}else{
			try {
				getDeptInfoService().delete(deptInfo);
			} catch (Exception e) {
				e.printStackTrace();
				this.addActionError("发生错误："+e);
				result = ERROR;
			}
		}
		result = checkAjax(result);
		return result;
	}
	
	/**
	 * 获取远程关联部门树列表 
	 * @return
	 */
	@Deprecated
	public String getRelevanceDeptTreeList(){
		
		try {
			String relevanceConfig = deptInfo.getRelevanceConfig();
			List<Map<String, Object>> list = getDeptInfoService().getRelevanceDeptTreeList(relevanceConfig, 1);
			mapResult.put("result", list);
			mapResult.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			mapResult.put("success", false);
			mapResult.put("errorMessage", "发生错误："+e);
		}
		
		return "json";
	}

	/**
	 * 加载各参数的选择列表
	 * @param item
	 * @throws Exception
	 */
	private void loadParamList(DeptInfo item) throws Exception {
		parentSelectList = getDeptInfoService().getParentSelectList(item.getPkId(), item.getParId());
		page.getCondition().put("parentNavList", parentSelectList);
		
		DeptTypeEnum[] deptTypes = getDeptInfoService().getSubDeptTypes(item.getParent());
		page.getCondition().put("deptTypes", deptTypes);
		
		orgTypeList = getDeptInfoService().getOrgTypeList();
		
		if(item.getOrg()!=null&&item.getOrg().getOrgType()!=null&&item.getOrg().getOrgType().getPkId()!=null){
			orgDeptLevelList = getDeptInfoService().getOrgDeptLevelList(item.getOrg().getOrgType().getPkId());
		}else{
			orgDeptLevelList = new ArrayList<OrgDeptLevel>();
		}
	}
	
	public String loadOrgDeptLevelList(){
		try {
			orgDeptLevelList = getDeptInfoService().getOrgDeptLevelList(deptInfo.getOrg().getOrgType().getPkId());
			mapResult.put("orgDeptLevelList", orgDeptLevelList);
			mapResult.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mapResult.put("success", false);
			mapResult.put("errorMessage", e.getMessage());
		}
		return "json";
	}
	
	public String loadParentOrg(){
		try {
			OrgInfo org = getDeptInfoService().getParentOrgByDeptId(deptInfo.getPkId());
			mapResult.put("orgName", org.getName());
			mapResult.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mapResult.put("success", false);
			mapResult.put("errorMessage", e.getMessage());
		}
		return "json";
	}

	/**
	 * 保存部门
	 * @author lzyyj
	 * @date 2016年11月21日 17:08:54
	 * {"item.pkId":pkId,"item.parId":parentId,"item.name":name,"item.deptNo":deptNo,"item.deptType":deptType,"item.org.rootDept.pkId":rootDeptId,"item.relevanceConfig":relevanceConfig,"item.org.name":fullName}
	 */
	public String save(){
		String result = SUCCESS;
		
		setItemPropertyList("parId,name,deptNo,deptType,relevanceConfig,org.orgType");

		try {
			deptInfo= getDeptInfoService().save(deptInfo, itemPropertyList);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("发生错误："+e);
			result = ERROR;
		}
		
		result = checkAjax(result);
		return result;
	}

	/**
	 * 检查ajax调用，并针对ajax调用调整返回结果
	 * @param retResult
	 * @return
	 */
	public String checkAjax(String retResult) {
		if (isAjax()){
			if (hasActionErrors()){
				mapResult.put("success",false);
				mapResult.put("errorMessage", ""+this.getActionErrors());
			}else{
				mapResult.put("success",true);
				mapResult.put("message", ""+getActionMessages());
			}
			retResult = "json";
		}
		return retResult;
	}

	private void copyProperties(DeptInfo deptInfo) {
		deptInfo.setDeptNo(deptNo);
		deptInfo.setName(name);
		deptInfo.setType(type);
		deptInfo.setParId(parId);
		deptInfo.setDeptType(deptType);
	}
	
	public String getNowpage() {
		return nowpage;
	}

	public void setNowpage(String nowpage) {
		this.nowpage = nowpage;
	}

	public String getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(String maxpage) {
		this.maxpage = maxpage;
	}

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public List<DeptInfo> getResult() {
		return result;
	}

	public void setResult(List<DeptInfo> result) {
		this.result = result;
	}

	public int getOrgId() {
		return orgId;
	}

	public void setOrgId(int orgId) {
		this.orgId = orgId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}
	public int getDpIpId() {
		return dpIpId;
	}
	public void setDpIpId(int dpIpId) {
		this.dpIpId = dpIpId;
	}
	
	public List<OrgInfo> getOrginfolist() {
		return orginfolist;
	}
	public void setOrginfolist(List<OrgInfo> orginfolist) {
		this.orginfolist = orginfolist;
	}
	public List<DeptInfo> getDeptInfolist() {
		return deptInfolist;
	}
	public void setDeptInfolist(List<DeptInfo> deptInfolist) {
		this.deptInfolist = deptInfolist;
	}
	public String getOrgInfoJson() {
		return orgInfoJson;
	}
	public void setOrgInfoJson(String orgInfoJson) {
		this.orgInfoJson = orgInfoJson;
	}
	public List<UserDto> getUserBeanAll() {
		return userBeanAll;
	}
	public void setUserBeanAll(List<UserDto> userBeanAll) {
		this.userBeanAll = userBeanAll;
	}
	public String getIpAddress() {
		return ipAddress;
	}
	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
	public List<DeptInfoDto> getDeptInfoBeans() {
		return deptInfoBeans;
	}
	public void setDeptInfoBeans(List<DeptInfoDto> deptInfoBeans) {
		this.deptInfoBeans = deptInfoBeans;
	}
	public List<UserDto> getDeptUsers() {
		return deptUsers;
	}
	public void setDeptUsers(List<UserDto> deptUsers) {
		this.deptUsers = deptUsers;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getDeptNo() {
		return deptNo;
	}
	public void setDeptNo(String deptNo) {
		this.deptNo = deptNo;
	}
	public DeptInfo getDeptInfo() {
		return deptInfo;
	}
	public void setDeptInfo(DeptInfo deptInfo) {
		this.deptInfo = deptInfo;
	}
	public DeptInfo getItem() {
		return deptInfo;
	}
	public void setItem(DeptInfo deptInfo) {
		this.deptInfo = deptInfo;
	}
	
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public List<OrgType> getOrgTypeList() {
		return orgTypeList;
	}

	public void setOrgTypeList(List<OrgType> orgTypeList) {
		this.orgTypeList = orgTypeList;
	}

	public List<OrgDeptLevel> getOrgDeptLevelList() {
		return orgDeptLevelList;
	}

	public void setOrgDeptLevelList(List<OrgDeptLevel> orgDeptLevelList) {
		this.orgDeptLevelList = orgDeptLevelList;
	}

}
